<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="分类" prop="categoryId">
				<!-- <el-select v-model="queryParams.categoryName" placeholder="请选择分类" clearable @change="handleQuery">
					<el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select> -->
				<el-input v-model="queryParams.categoryName" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="手机号码" prop="tel">
				<el-input v-model="queryParams.tel" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['biz:store:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['biz:store:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['biz:store:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['biz:store:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="图片" align="center" prop="imgUrl">
				<template slot-scope="scope">
					<img :src="$store.state.app.imgUrl +scope.row.imgUrl" style="width:100px" alt="">
				</template>
			</el-table-column>
			<el-table-column label="名称" align="center" prop="name" />
			<el-table-column label="管理员" align="center" prop="userNickName" />
			<el-table-column label="手机号码" align="center" prop="tel" />
			<el-table-column label="分类" align="center" prop="categoryName" />
			<el-table-column label="地址" align="center" prop="address" />
			<!-- <el-table-column label="营业时间" align="center" prop="openTime" /> -->
			<!-- <el-table-column label="logo" align="center" prop="logoUrl" /> -->
			<el-table-column label="剩余积分" align="center" prop="remainPoints" />
			<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:store:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['biz:store:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改自营商城门店管理对话框 -->
		<el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="80vw" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="180px">
				<el-form-item label="名称" prop="name" required>
					<el-input v-model="form.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="管理员" prop="userId" required>
					<el-select :loading="loading" :remote-method="remoteMethod" clearable="" remote filterable v-model="form.userId" placeholder="请选择">
						<el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="分类 " prop="categoryId" required>
					<el-select v-model="form.categoryId" placeholder="请选择分类">
						<el-option v-for="item in typeList" :key="item.id.toString()" :label="item.name" :value="item.id.toString()"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="unit">
					<el-switch v-model="form.status" active-text="启用" inactive-text="停用" :active-value="1" :inactive-value="0">
					</el-switch>
				</el-form-item>
				<el-form-item label="手机号码" prop="tel" required>
					<el-input v-model="form.tel" type="number" placeholder="请输入手机号码" />
				</el-form-item>
				<el-form-item label="公共商品额度" prop="subsidyAllVipGoodsAmount" required>
					<el-input v-model="form.subsidyAllVipGoodsAmount" type="number" placeholder="请输入公共商品额度" />
				</el-form-item>
				<el-card style="margin-bottom:20px">
					<h1>消费补贴金</h1>
					<el-form-item label="赠送会员单次补贴额度" prop="subsidyGiftAmount" required>
						<el-input v-model="form.subsidyGiftAmount" type="number" placeholder="请输入赠送会员单次补贴额度" />
					</el-form-item>
					<el-form-item label="可赠送补贴总额度" prop="subsidyRemainAmount" required>
						<el-input v-model="form.subsidyRemainAmount" type="number" placeholder="请输入可赠送总额度" />
					</el-form-item>
				</el-card>
				<el-card style="margin-bottom:20px">
					<h1>赠送积分</h1>
					<el-form-item label="赠送会员单次补贴额度" prop="subsidyGiftAmount" required>
						<el-input v-model="form.subsidyGiftAmount" type="number" placeholder="请输入赠送会员单次补贴额度" />
					</el-form-item>
					<el-form-item label="可赠送补贴总额度" prop="subsidyRemainAmount" required>
						<el-input v-model="form.subsidyRemainAmount" type="number" placeholder="请输入可赠送总额度" />
					</el-form-item>
				</el-card>
				<!-- <el-form-item label="实际加盟金额" prop="subsidyJoiningAmount" required>
					<el-input v-model="form.subsidyJoiningAmount" type="number" placeholder="请输入实际加盟金额" />
				</el-form-item> -->
				<el-form-item label="内部会员商品额度" prop="subsidyLocalVipGoodsAmount" required>
					<el-input v-model="form.subsidyLocalVipGoodsAmount" type="number" placeholder="请输入内部会员商品额度" />
				</el-form-item>
				<el-form-item label="地址" prop="address" :required="true">
					<el-input v-model="form.address" placeholder="请输入地址"></el-input>
					<!-- {{inputModel.addr}} -->
					<el-button size="medium" type="primary" @click="handleAddress" :loading="loading">
						选择地址
					</el-button>
				</el-form-item>
				<el-form-item label="经纬度" :required="false">
					{{form.longitude}}--{{form.latitude}}
				</el-form-item>
				<!-- <el-form-item label="营业时间" prop="openTime">
          <el-input v-model="form.openTime" placeholder="请输入营业时间" />
        </el-form-item> -->
				<el-form-item label="描述" prop="remark">
					<el-input v-model="form.remark" placeholder="请输入描述" />
				</el-form-item>
				<el-form-item label="图片" prop="imgUrl">
					<ImageUpload :limit='1' ref="ImageUpload"></ImageUpload>
				</el-form-item>
				<el-form-item label="详情">
					<editor v-model="form.content" :min-height="192" />
				</el-form-item>
				<!-- <el-form-item label="剩余积分" prop="remainPoints">
					<el-input v-model="form.remainPoints" placeholder="请输入剩余积分" />
				</el-form-item> -->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
			<addressMap ref="addressMap" @confirm='confirmAdress'></addressMap>
		</el-dialog>
	</div>
</template>

<script>
import {
	listStore,
	getStore,
	delStore,
	addStore,
	updateStore,
} from '@/api/biz/subsidy';
import { listStoreCategory } from '@/api/biz/subsidy';
import { listNoStoreUserByRoleKey } from '@/api/system/user';
import addressMap from '@/components/addressMap';
export default {
	components: {
		addressMap,
	},
	name: 'Store',
	data() {
		return {
			// 分类列表
			typeList: [],
			userList: [],
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 自营商城门店管理表格数据
			storeList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				type: null,
				categoryId: null,
				name: null,
				tel: null,
				address: null,
				latitude: null,
				longitude: null,
				openTime: null,
				imgUrl: null,
				logoUrl: null,
				content: null,
				status: null,
				readCount: null,
				visitCount: null,
				remainPoints: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
		};
	},
	created() {
		this.listNoStoreUserByRoleKey();
		this.getList();
		this.getType();
	},
	activated() {
		this.getType();
	},
	methods: {
		remoteMethod(query) {
			if (query !== '') {
				this.loading = true;
				const params = {
					nickName: query,
					pageSize: 10,
					roleKey: 'subsidy',
				};
				listNoStoreUserByRoleKey(params).then((response) => {
					this.userList = response.data;
					this.loading = false;
				});
			} else {
				this.userList = [];
			}
		},
		listNoStoreUserByRoleKey(params) {
			listNoStoreUserByRoleKey({
				pageSize: 10,
				roleKey: 'subsidy',
			}).then((response) => {
				this.userList = response.data;
			});
		},
		getType() {
			listStoreCategory({
				pageSize: 100,
			}).then((response) => {
				this.typeList = response.rows;
			});
		},
		confirmAdress(res) {
			console.log(res);
			this.$set(this.form, 'address', res.inputModel.poiaddress);

			this.form.latitude = String(res.inputModel.latlng.lat);
			this.form.longitude = String(res.inputModel.latlng.lng);
		},
		handleAddress() {
			this.$refs.addressMap.show();
		},
		/** 查询自营商城门店管理列表 */
		getList() {
			this.loading = true;
			listStore(this.queryParams).then((response) => {
				this.storeList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				type: null,
				categoryId: null,
				name: null,
				tel: null,
				address: null,
				latitude: null,
				longitude: null,
				openTime: null,
				imgUrl: null,
				logoUrl: null,
				content: null,
				status: 0,
				createByName: null,
				createById: null,
				createTime: null,
				updateByName: null,
				updateById: null,
				updateTime: null,
				isDeleted: null,
				readCount: null,
				visitCount: null,
				remark: null,
				remainPoints: null,
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map((item) => item.id);
			this.single = selection.length !== 1;
			this.multiple = !selection.length;
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '添加自营商城门店管理';
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id || this.ids;
			this.open = true;
			getStore(id).then((response) => {
				this.$refs.ImageUpload.fileList = [
					{
						url:
							this.$store.state.app.imgUrl + response.data.imgUrl,
						uploadUrl: response.data.imgUrl,
					},
				];
				this.form = response.data;

				this.title = '修改自营商城门店管理';
			});
		},
		/** 提交按钮 */
		submitForm() {
			if (this.$refs.ImageUpload.fileList.length) {
				var fileList = this.$refs.ImageUpload.fileList[0];
				this.form.imgUrl = fileList.uploadUrl;
			}
			this.form.subsidyJoiningAmount = 1;
			this.$refs['form'].validate((valid) => {
				if (valid) {
					if (this.form.id != null) {
						updateStore(this.form).then((response) => {
							this.$modal.msgSuccess('修改成功');
							this.open = false;
							this.getList();
						});
					} else {
						addStore(this.form).then((response) => {
							this.$modal.msgSuccess('新增成功');
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids;
			this.$modal
				.confirm(
					'是否确认删除自营商城门店管理编号为"' + ids + '"的数据项？'
				)
				.then(function () {
					return delStore(ids);
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				'biz/store/export',
				{
					...this.queryParams,
				},
				`store_${new Date().getTime()}.xlsx`
			);
		},
	},
};
</script>
